﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Text.RegularExpressions;

public partial class wikia : System.Web.UI.Page
{

    protected String cpageName = String.Empty;
    protected String cDescription = String.Empty;
    protected String cTitle = String.Empty;
    protected Int32 catid = 0;
    private int iTotalPages = 0;
    private int iPageSize = 6;
    public int CurrentPage = 1;

    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlMeta meta = new HtmlMeta();
        meta.Name = "keywords";
        meta.Content = PageHeaderData.GetPageKeywordsByType(NewsType.Wiki);
        this.Header.Controls.Add(meta);
        GetParameters();
        try
        {

            if (cpageName.Length > 0)
            {
                
                CategoryItem arr = Category.ItemByPageName(cpageName);
                if (arr != null)
                {
                    cTitle = arr.cTitle;
                    catid = arr.catId;
                    cpageName = arr.cPageName;
                    cDescription = arr.cDescription;
                    Title = arr.cTitle;
                }
                arr = Category.ItemById(arr.Ancestor);
                if (arr.Ancestor != -1)
                {
                    string sLink=string.Format("<img src=\"images/bullet4.gif\" />" +
                            "<a href='{0}-w.aspx'>" +
                            "{1}</a>",HttpUtility.UrlEncode(arr.cPageName),arr.cTitle);
                    divTitleLink.InnerHtml += sLink;
                    
                    arr = Category.ItemById(arr.Ancestor);
                    
                }
                GetNewsData();
                SetSubCategorys();

                ucHeader1.index = "5";
            }
        }
        catch { }
    }

    private void GetParameters()
    {
        cpageName = (Request.Params["c"] == null) ? string.Empty : Request.Params["c"].ToString();
        if (cpageName.Length > 0)
        {
            string sPage = string.Empty;
            try
            {
                sPage = cpageName.Split('^')[1];
                Regex rx = new Regex(@"([\d]*)");
                Match m = rx.Match(sPage);
                if (m.Success)
                {
                    int.TryParse(Regex.Replace(sPage, @"[^\d]", ""), out CurrentPage);
                }
            }
            catch { }
            cpageName = cpageName.Split('^')[0];
        }
    }

    public void SetSubCategorys()
    {
        StringBuilder strLinks = new StringBuilder();
        if (catid != 0)
        {
            List<CategoryItem> subCategorys = Category.ByAncestors(catid);
            if (subCategorys != null)
            {
                foreach (CategoryItem subc in subCategorys)
                {
                    string sText = string.Format("<li><a href=\"{0}-w.aspx\">{1}</a></li>", HttpUtility.UrlEncode(subc.cPageName), subc.cTitle);
                    strLinks.Append(sText);
                }
                ulSubCategorys.InnerHtml+=strLinks.ToString();
                if(subCategorys.Count==0)
                divSubCategory.Visible = false;
            }
            else
            {
                //if(subCategorys.Count==0)
                divSubCategory.Visible = false;
            }

        }
    }

    private void GetNewsData() 
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(catid);//CatID
        Dal.arParams.Add(CurrentPage);//Current Page
        Dal.arParams.Add(iPageSize);//PageSize
        DataTable DT = Dal.ExecuteDBsp("upd_GetWikiByCategoryWPaging", Dal.arParams);
        if (DT != null)
        {
            if (DT.Rows.Count > 0)
            {
                iTotalPages = (int)Math.Ceiling((double)Convert.ToInt32(DT.Rows[0]["TotalRows"]) / iPageSize);
                gvNews.DataSource = DT;
                gvNews.DataBind();

                SimplePager();
            }
        }
    }

    public void SimplePager()
    {
        if (CurrentPage < 0) CurrentPage = 1;
        if (iTotalPages < 0) iTotalPages = 0;

        var pager = new PagerBuilder(cpageName);

        if (CurrentPage > 1)
        {
            pager.AddPage("&lt;&lt;", 1);
            pager.AddPage("&lt;", CurrentPage - 1);
        }

        var start = Math.Max(CurrentPage - 2, 1);
        var end = Math.Min(iTotalPages, CurrentPage + 2);

        for (var i = start; i <= end; i++)
        {
            if (i == CurrentPage)
                pager.AddPage(i.ToString(), i, "font-weight: bold; color: #0b61a4;");
            else
                pager.AddPage(i.ToString(), i);
        }

        if (CurrentPage < iTotalPages)
        {
            pager.AddPage("&gt;", CurrentPage + 1);
            pager.AddPage("&gt;&gt;", iTotalPages);
        }

        nav6.InnerHtml += pager.ToString();
    }

    public class PagerBuilder
    {
        private class PagerLink
        {
            public string Title { get; set; }
            public int PageNo { get; set; }
            public string Class { get; set; }
        }

        private readonly string _urlTemplate;
        private readonly List<PagerLink> _pagerLinks =
                new List<PagerLink>();

        public PagerBuilder(string urlTemplate)
        {
            _urlTemplate = urlTemplate;
        }

        public string PagerClass { get; set; }

        public void AddPage(string title, int pageNo)
        {
            AddPage(title, pageNo, string.Empty);
        }

        public void AddPage(string title, int pageNo,
                            string itemClass)
        {
            var link = new PagerLink
            {
                PageNo = pageNo,
                Title = title,
                Class = itemClass
            };
            _pagerLinks.Add(link);
        }

        public override string ToString()
        {
            var builder = new StringBuilder();

            foreach (var link in _pagerLinks)
            {
                builder.Append("<li>");
                builder.Append("<a");
                if (link.Class != string.Empty)
                {
                    builder.AppendFormat(" style=\"{0}\"", link.Class);
                }
                builder.Append(" href=\"");
                builder.AppendFormat("{0}^p_{1}-w.aspx", _urlTemplate, link.PageNo);
                builder.Append("\">");
                builder.Append(link.Title);
                builder.Append("</a>");
                builder.Append("</li>");
            }
            return builder.ToString();
        }
    }
}
